home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0992.ARJ / CORONER.C < prev    next >
C/C++ Source or Header  |  1992-06-07  |  4KB  |  159 lines

  1. //=================================
  2. // Coroner, by Matt Pietrek, 1992
  3. // File: CORONER.C
  4. // from "Undocumented Windows"
  5. // (Chapter 10)
  6. //=================================
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "coroner.h"
  12.  
  13. char AppName[]      = "CORONER";
  14.  
  15. char LogFileName[MAX_PATH_LENGTH];
  16. char ExceptionTaskName[14];
  17.  
  18. HANDLE HInstance;
  19. HANDLE HCoronerWnd;
  20. char ERROR_CAPTION[] = "Problem!!!";
  21.  
  22.  
  23. long FAR PASCAL _export CoronerDialogProc(HWND hDlg, WORD message, 
  24.     WORD wParam, DWORD lParam)
  25. {
  26.     char buffer[128];
  27.     
  28.     switch ( message )
  29.     {
  30.         case WM_COMMAND:
  31.             if ( wParam == IDOK )
  32.             {
  33.                     CloseWindow(hDlg);
  34.                     return TRUE;
  35.             }
  36.             break;
  37.  
  38.         case WM_CORONER_FILEOPEN_ERROR :
  39.             MessageBox
  40.             (
  41.                 hDlg,
  42.                 "CORONER could not open a .LOG file",
  43.                 ERROR_CAPTION,
  44.                 MB_OK
  45.             );
  46.             break;
  47.                     
  48.     
  49.         case WM_CORONER_EXCEPTION :
  50.             sprintf(buffer, "Exception %u in %s", wParam, ExceptionTaskName);
  51.             MessageBox(hDlg, buffer, AppName, MB_OK);
  52.             break;
  53.                 
  54.         case WM_DESTROY:
  55.             PostQuitMessage(0);
  56.             return 0;
  57.     }
  58.  
  59.     return DefWindowProc(hDlg, message, wParam, lParam);
  60. }
  61.  
  62. void GetProgramVariables(void)
  63. {
  64.     char WindowsDirectory[MAX_PATH_LENGTH];
  65.     int i;
  66.  
  67.     GetWindowsDirectory(WindowsDirectory, MAX_PATH_LENGTH);
  68.     
  69.     i = strlen(WindowsDirectory);
  70.     
  71.     if ( WindowsDirectory[i-1] != '\\' )
  72.     {   // Tack on a '\' if there isn't one
  73.         WindowsDirectory[i] = '\\';
  74.         WindowsDirectory[i+1] = 0;
  75.     }
  76.     
  77.     strcpy(LogFileName, WindowsDirectory);
  78.     strcat(LogFileName, "CORONER.LOG"); 
  79. }
  80.  
  81. int RegisterCoronerWindowClass(void)
  82. {
  83.     WNDCLASS wndclass;
  84.     
  85.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  86.     (FARPROC)wndclass.lpfnWndProc = (FARPROC)CoronerDialogProc;
  87.     wndclass.cbClsExtra = 0;
  88.     wndclass.cbWndExtra = DLGWINDOWEXTRA;
  89.     wndclass.hInstance = HInstance;
  90.     wndclass.hIcon = LoadIcon(HInstance,"CORONERICON");
  91.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  92.     wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  93.     wndclass.lpszMenuName = NULL;
  94.     wndclass.lpszClassName = AppName;
  95.     
  96.     return RegisterClass(&wndclass);    
  97. }
  98.  
  99. void CoronerError(char *msg)
  100. {
  101.     char buffer[128];
  102.     
  103.     sprintf(buffer, "CORONER %s", msg);
  104.     
  105.     MessageBox(NULL, buffer, ERROR_CAPTION, MB_OK);
  106. }
  107.  
  108. #pragma argsused
  109.  
  110. int PASCAL WinMain( HANDLE hInstance,  HANDLE hPrevInstance,
  111.                     LPSTR lpszCmdLine, int nCmdShow)
  112.  
  113. {
  114.     HWND hWnd;
  115.     MSG msg;
  116.     
  117.     HInstance = hInstance;
  118.     
  119.     if ( hPrevInstance )
  120.     {
  121.         CoronerError("can only be run once");
  122.         return 0;
  123.     }
  124.  
  125.     if ( RegisterCoronerWindowClass() == 0)
  126.     {
  127.         CoronerError("can't create it's window class");
  128.         return 0;
  129.     }
  130.     
  131.     HCoronerWnd = hWnd = CreateDialog( HInstance, "CORONER", NULL, NULL);
  132.  
  133.     if ( hWnd == 0 )
  134.     {
  135.         CoronerError("can't create it's window");
  136.         return 0;
  137.     }
  138.     
  139.     ShowWindow(hWnd, SW_MINIMIZE);
  140.  
  141.     GetProgramVariables();
  142.  
  143.     if ( SetupInterruptHandler() == FALSE )
  144.     {
  145.         CoronerError("can't install a fault handler");
  146.         return 0;
  147.     }
  148.  
  149.     while ( GetMessage(&msg, NULL, 0, 0) )
  150.     {
  151.         TranslateMessage(&msg);
  152.         DispatchMessage(&msg);
  153.     }
  154.     
  155.     ShutdownInterruptHandler();
  156.  
  157.     return 0;
  158. }
  159.